/*
 * SelectCustomer.java
 *
 * Created on February 22, 2007, 1:12 PM
 * @author Jeff Jenkins
 * @descritption: This method provides a user intereface to select a customer to view or update
 * the customer. Or allows you to create a customer. It also contains
 * funtinality to create a new order for a customer or for the default customer
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * This form is created after the login page, which has various service options in it.
 * @author Jeff
 * This method provides a user intereface to select a customer to view or update
 * the customer. Or allows you to create a customer. It also contains
 * funtinality to create a new order for a customer or for the default customer
 */
public class SelectCustomer extends javax.swing.JFrame {
    //A list of all the customers
    List li;
    
    //Currenent Operations BO for this register
    
    CurrentOptsBO currentOpts;
    
    //Singleton variable
    private static SelectCustomer instance = null;
    
    /** Creates new form SelectCustomer
     *  I choose to implement the singleton pattern
     *  so if in the futer if any other windows call this window they will
     *  only be allowed to have one at a atime*/
    private SelectCustomer() {
        initComponents();
        this.pack();
        populateList();
       this.setLocationRelativeTo(null);
        bannerPanel.setPreferredSize(new java.awt.Dimension(800, 75));
        JLabelPic.setIcon(new javax.swing.ImageIcon(getClass().getResource("logoWeb5.gif")));
        bannerPanel.add(JLabelPic);
        
        
    }
    
    /**
     * implements singleton pattern to insure that only one instance of this
     * gui is created
     * @return instance
     */
    
    public static synchronized SelectCustomer getInstance() {
        
        if (instance == null) {
            instance = new SelectCustomer();
        }
        
        return instance;
    }
    
    void populateList(){
        try{
            cmbCust.removeAllItems();
            li = CustomerDAO.getInstance().getAll();
            for(int i = 0; i<li.size(); i++){
                
                cmbCust.addItem(((String[])li.get(i))[2] + ", " + ((String[])li.get(i))[1]+ "                  " + ((String[])li.get(i))[3]);
                this.setVisible(true);
            }
        }catch(DataException ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null,ex.toString(),"Error populating List", JOptionPane.ERROR_MESSAGE);
            
        }
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel8 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        cmbCust = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        btnNewOrder = new javax.swing.JButton();
        btnSaleReturn = new javax.swing.JButton();
        btnEditOrder = new javax.swing.JButton();
        btnRentalReturn = new javax.swing.JButton();
        btnView = new javax.swing.JButton();
        btnNew = new javax.swing.JButton();
        btnBatch = new javax.swing.JButton();
        btnLogOut = new javax.swing.JButton();
        bannerPanel = new javax.swing.JPanel();
        JLabelPic = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.BorderLayout(15, 15));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Information");
        jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 5));

        jPanel8.setPreferredSize(new java.awt.Dimension(700, 350));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(420, 230));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel5.setPreferredSize(new java.awt.Dimension(200, 40));
        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel7.setPreferredSize(new java.awt.Dimension(300, 20));
        jLabel1.setText("Choose Customer to View or Edit:");
        jLabel1.setPreferredSize(new java.awt.Dimension(300, 14));
        jPanel7.add(jLabel1);

        jPanel5.add(jPanel7, java.awt.BorderLayout.WEST);

        jPanel1.add(jPanel5, java.awt.BorderLayout.NORTH);

        jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(15, 15, 15, 15));
        jPanel6.setPreferredSize(new java.awt.Dimension(100, 40));
        cmbCust.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Choose or Type In Name>" }));
        cmbCust.setPreferredSize(new java.awt.Dimension(350, 20));
        jPanel6.add(cmbCust);

        jPanel1.add(jPanel6, java.awt.BorderLayout.CENTER);

        jPanel8.add(jPanel1);

        jPanel2.setPreferredSize(new java.awt.Dimension(200, 230));
        btnNewOrder.setText("New Order");
        btnNewOrder.setOpaque(false);
        btnNewOrder.setPreferredSize(new java.awt.Dimension(135, 23));
        btnNewOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewOrderActionPerformed(evt);
            }
        });

        jPanel2.add(btnNewOrder);

        btnSaleReturn.setText("Sale Return");
        btnSaleReturn.setPreferredSize(new java.awt.Dimension(135, 23));
        btnSaleReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaleReturnActionPerformed(evt);
            }
        });

        jPanel2.add(btnSaleReturn);

        btnEditOrder.setText("View/Edit Order");
        btnEditOrder.setPreferredSize(new java.awt.Dimension(135, 23));
        btnEditOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditOrderActionPerformed(evt);
            }
        });

        jPanel2.add(btnEditOrder);

        btnRentalReturn.setText("Return Rental");
        btnRentalReturn.setPreferredSize(new java.awt.Dimension(135, 23));
        btnRentalReturn.setRequestFocusEnabled(false);
        btnRentalReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRentalReturnActionPerformed(evt);
            }
        });

        jPanel2.add(btnRentalReturn);

        btnView.setText(" View/Edit Cust.");
        btnView.setPreferredSize(new java.awt.Dimension(135, 23));
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        jPanel2.add(btnView);

        btnNew.setText("New Customer");
        btnNew.setPreferredSize(new java.awt.Dimension(135, 23));
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        jPanel2.add(btnNew);

        btnBatch.setText("Batch Processes");
        btnBatch.setPreferredSize(new java.awt.Dimension(135, 23));
        btnBatch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatchActionPerformed(evt);
            }
        });

        jPanel2.add(btnBatch);

        btnLogOut.setText("Log Out");
        btnLogOut.setPreferredSize(new java.awt.Dimension(135, 23));
        btnLogOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogOutActionPerformed(evt);
            }
        });

        jPanel2.add(btnLogOut);

        jPanel8.add(jPanel2);

        getContentPane().add(jPanel8, java.awt.BorderLayout.CENTER);

        bannerPanel.add(JLabelPic);

        getContentPane().add(bannerPanel, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnEditOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditOrderActionPerformed
        try{
            // Retrives Customer ID
            String id = ((String[])li.get((cmbCust.getSelectedIndex())))[0];
            
            //Gets Customer Object
            
            CustomerBO cust = CustomerDAO.getInstance().read(id);
            
            GetTransaction.getInstance().getTransaction(cust);
         
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Cannot get product", JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_btnEditOrderActionPerformed
    
    private void btnLogOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogOutActionPerformed
        Cache.getInstance().remove("currentOpts");
        this.dispose();
    }//GEN-LAST:event_btnLogOutActionPerformed
    
    private void btnRentalReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRentalReturnActionPerformed
// TODO add your handling code here:
        RentalReturnGUI view = RentalReturnGUI.getInstance();
        view.setVisible(true);
        view.setLocationRelativeTo(null);
    }//GEN-LAST:event_btnRentalReturnActionPerformed

    private void btnSaleReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaleReturnActionPerformed
// TODO add your handling code here:
        //copy the basic format from newOrder...
        try{
            
            // Retrives Customer ID
            String id = ((String[])li.get((cmbCust.getSelectedIndex())))[0];
            
            //Gets Customer Object
            
            CustomerBO cust = CustomerDAO.getInstance().read(id);
            
            SaleReturn.getInstance().loadForm(cust);
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error Creating New Order", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaleReturnActionPerformed
    
    private void btnBatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBatchActionPerformed
// TODO add your handling code here:
        //show the batch process gui here
        BatchGUI view = (BatchGUI)RSGUIFACTORY.getInstance().getGUI("batch");
        
        // passes the revenueSource object to the sub view to populate is with the data that is uniqe to that revenue source
        view.populate();
    }//GEN-LAST:event_btnBatchActionPerformed
    
    private void btnNewOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewOrderActionPerformed
        try{
            
            // Retrives Customer ID
            String id = ((String[])li.get((cmbCust.getSelectedIndex())))[0];
            
            //Gets Customer Object
            
            CustomerBO cust = CustomerDAO.getInstance().read(id);
            
            NewOrder.getInstance().loadForm(cust);
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error Creating New Order", JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_btnNewOrderActionPerformed
    
    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        try {
            
            // Retrives Customer ID
            String id = ((String[])li.get((cmbCust.getSelectedIndex())))[0];
            
            //Gets Customer Object
            
            CustomerBO cust = CustomerDAO.getInstance().read(id);
            
            //Passes to the next Screen
            CustomerView.getInstance().loadCustomer(cust, false);
            
        } catch (DataException ex) {
            JOptionPane.showMessageDialog(null,ex.toString(),"Error retrieving customer", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_btnViewActionPerformed
    
    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        try {
            
            // Gets new Customer
            CustomerBO cust = CustomerDAO.getInstance().create();
            
            //Passes to the next Screen
            CustomerView.getInstance().loadCustomer(cust, true);
            
        } catch (DataException ex) {
            JOptionPane.showMessageDialog(null,ex.toString(),"Error creating customer", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_btnNewActionPerformed
        /* This method retrieves the customer object for a certain customer and passes it to the view
         * customer GUI. */
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SelectCustomer().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLabelPic;
    private javax.swing.JPanel bannerPanel;
    private javax.swing.JButton btnBatch;
    private javax.swing.JButton btnEditOrder;
    private javax.swing.JButton btnLogOut;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnNewOrder;
    private javax.swing.JButton btnRentalReturn;
    private javax.swing.JButton btnSaleReturn;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cmbCust;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    // End of variables declaration//GEN-END:variables
    
}
